import { createStore } from "vuex";

const now = new Date();

export default createStore({
  state: {
    //当前用户
    user: {
      id: -1,
      name: "",
      avatar: "",
      email: "",
      token: "",
      expiration_time: -1,
      sex: -1,
      client_type: -1,
      bio: "",
    },

    //以下信息不应该存储在vuex中 仅仅测试用
    servers: [
      //servers下有多个server
      //例如server1
      {
        serverId: "1",
        serverName: "Yuanshen",
        serverImg: "../../assets/1.png",
        lastSessionId: "1_1", //上一次停留的会话
        //一个服务器有多个会话
      },
      {
        serverId: "2",
        serverName: "Study",
        serverImg: "../../assets/1.png",
        lastSessionId: "2_1",
      },
      {
        serverId: "3",
        serverName: "Yuanshen",
        serverImg: "../../assets/1.png",
        lastSessionId: "3_1", //上一次停留的会话
        //一个服务器有多个会话
      },
      {
        serverId: "4",
        serverName: "Yuanshen",
        serverImg: "../../assets/1.png",
        lastSessionId: "4_1", //上一次停留的会话
        //一个服务器有多个会话
      },
      {
        serverId: "5",
        serverName: "Yuanshen",
        serverImg: "../../assets/1.png",
        lastSessionId: "5_1", //上一次停留的会话
        //一个服务器有多个会话
      },
      {
        serverId: "6",
        serverName: "Yuanshen",
        serverImg: "../../assets/1.png",
        lastSessionId: "6_1", //上一次停留的会话
        //一个服务器有多个会话
      },
      {
        serverId: "7",
        serverName: "Yuanshen",
        serverImg: "../../assets/1.png",
        lastSessionId: "7_1", //上一次停留的会话
        //一个服务器有多个会话
      },
      {
        serverId: "8",
        serverName: "Yuanshen",
        serverImg: "../../assets/1.png",
        lastSessionId: "8_1", //上一次停留的会话
        //一个服务器有多个会话
      },
      {
        serverId: "9",
        serverName: "Yuanshen",
        serverImg: "../../assets/1.png",
        lastSessionId: "9_1", //上一次停留的会话
        //一个服务器有多个会话
      },
      {
        serverId: "10",
        serverName: "Yuanshen",
        serverImg: "../../assets/1.png",
        lastSessionId: "10_1", //上一次停留的会话
        //一个服务器有多个会话
      },
      {
        serverId: "11",
        serverName: "Yuanshen",
        serverImg: "../../assets/1.png",
        lastSessionId: "11_1", //上一次停留的会话
        //一个服务器有多个会话
      },
      {
        serverId: "12",
        serverName: "Yuanshen",
        serverImg: "../../assets/1.png",
        lastSessionId: "12_1", //上一次停留的会话
        //一个服务器有多个会话
      },
    ],

    sessions: [
      {
        sessionId: "1_1",
        belongServerId: "1", //所属服务器
        sessionName: "公告",
        sessionImg: "../../assets/1.png",
        //一个会话有多条message
        messages: [
          {
            senderID: "1",
            senderName: "Lebin",
            senderImg: "../assets/1.png",
            data: now,
            content: "hello,welcome to discord!",
          },
          {
            senderID: "2",
            senderName: "Kirby0",
            senderImg: "../assets/1.png",
            data: now,
            content: "Nice to meet you!",
          },
          {
            senderID: "3",
            senderName: "王子轩",
            senderImg: "../assets/1.png",
            data: now,
            content: "Hello.",
          },
          {
            senderID: "4",
            senderName: "李剑峰",
            senderImg: "../assets/1.png",
            data: now,
            content: "Im Jianfeng li.",
          },
          {
            senderID: "1",
            senderName: "Lebin",
            senderImg: "../assets/1.png",
            data: now,
            content:
              "hello,welcome to discord!hello,welcome to discord!hello,welcome to discord!hello,welcome to discord!hello,welcome to discord!",
          },
          {
            senderID: "1",
            senderName: "Lebin",
            senderImg: "../assets/1.png",
            data: now,
            content: "hello,welcome to discord!",
          },
          {
            senderID: "1",
            senderName: "Lebin",
            senderImg: "../assets/1.png",
            data: now,
            content: "hello,welcome to discord!",
          },
          {
            senderID: "1",
            senderName: "Lebin",
            senderImg: "../assets/1.png",
            data: now,
            content: "hello,welcome to discord!",
          },
          {
            senderID: "1",
            senderName: "Lebin",
            senderImg: "../assets/1.png",
            data: now,
            content: "hello,welcome to discord!",
          },
          {
            senderID: "1",
            senderName: "Lebin",
            senderImg: "../assets/1.png",
            data: now,
            content: "hello,welcome to discord!",
          },
          {
            senderID: "1",
            senderName: "Lebin",
            senderImg: "../assets/1.png",
            data: now,
            content: "hello,welcome to discord!",
          },
          {
            senderID: "1",
            senderName: "Lebin",
            senderImg: "../assets/1.png",
            data: now,
            content: "hello,welcome to discord!",
          },
          {
            senderID: "1",
            senderName: "Lebin",
            senderImg: "../assets/1.png",
            data: now,
            content: "hello,welcome to discord!",
          },
          {
            senderID: "1",
            senderName: "Lebin",
            senderImg: "../assets/1.png",
            data: now,
            content: "hello,welcome to discord!",
          },
          {
            senderID: "1",
            senderName: "Lebin",
            senderImg: "../assets/1.png",
            data: now,
            content: "hello,welcome to discord!",
          },
          {
            senderID: "1",
            senderName: "Lebin",
            senderImg: "../assets/1.png",
            data: now,
            content: "hello,welcome to discord!",
          },
        ],
      },
      //session2
      {
        sessionId: "1_2",
        belongServerId: "1",
        sessionName: "综合",
        sessionImg: "../../assets/1.png",
        //一个会话有多条message
        messages: [
          {
            senderID: "1",
            senderName: "Lebin",
            senderImg: "../assets/1.png",
            data: now,
            content: "hello,welcome to discord!",
          },
          {
            senderID: "2",
            senderName: "Kirby0",
            senderImg: "../assets/1.png",
            data: now,
            content: "Nice to meet you!",
          },
        ],
      },
      {
        sessionId: "2_1",
        belongServerId: "2", //所属服务器
        sessionName: "happpy",
        sessionImg: "../../assets/1.png",
        //一个会话有多条message
        messages: [
          {
            senderID: "1",
            senderName: "Lebin",
            senderImg: "../assets/1.png",
            data: now,
            content:
              "hello,welcome to discord!hello,welcome to discord!hello,welcome to discord!hello,welcome to discord!hello,welcome to discord!",
          },
          {
            senderID: "1",
            senderName: "Lebin",
            senderImg: "../assets/1.png",
            data: now,
            content: "hello,welcome to discord!",
          },
          {
            senderID: "1",
            senderName: "Lebin",
            senderImg: "../assets/1.png",
            data: now,
            content: "hello,welcome to discord!",
          },
        ],
      },
      { sessionId: "12", sessionName: "LiJianfeng", messages: [] },
      {
        sessionId: "13",
        sessionName: "AoWei",
        messages: [],
      },
    ],
    //当前服务器
    CurrentServerId: "@me",
    //当前会话
    CurrentSessionId: "",
    //筛子
    filterKey: "",
    //chat
    friend: [],
  },
  getters: {
    //获取状态
    CurrentUser: (state) => {
      return state.user;
    },
    CurrentServerId: (state) => {
      return state.CurrentServerId;
    },
    CurrentSessionId: (state) => {
      return state.CurrentSessionId;
    },
    //获取对应的json数据，json数据会提取的，
    // 例如渲染serverList时只需要提取serverId,serverImg,serverName,
    //而sessions就不需要，这里后面再说
    serverList: (state) => {
      return state.servers;
    },
    sessionList: (state) => {
      return state.sessions.filter(
        (item) => item.belongServerId === state.CurrentServerId
      );
    },
    session: (state) => {
      let newArray = state.sessions.filter(
        (item) => item.sessionId === state.CurrentSessionId
      );
      return newArray[0];
    },
  },
  mutations: {
    // //初始化数据
    // INIT_DATA(state) {
    //   let data = localStorage.getItem("mydiscord3");
    //   if (data) {
    //     state.servers = JSON.parse(data);
    //   }
    // },
    INIT_USER(state, data) {
      state.user = data;
    },
    //选择服务器
    SELECT_SERVER(state, id) {
      state.CurrentServerId = id;
    },
    //选择会话
    SELECT_SESSION(state, id) {
      state.CurrentSessionId = id;
    },
    //选择会话
    CREATE_SESSION(state, session) {
      state.sessions.push(session);
    },
    CHAT(state, friend) {
      state.friend = friend;
    },
  },

  modules: {},
});
